package com.easyaop.api.join;



import java.lang.reflect.Method;

import org.objectweb.asm.Type;

public class Join {
    Method method;
    Class clz;

    // MethodVo method;
    String signature;

    Object This;

    Object[] arguments;

    Object returnValue;

    Throwable throwable;

    public void setMethod(Method method) {
        this.method = method;
    }

    public Class getClz() {
        return clz;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public Object getThis() {
        return This;
    }

    public void setThis(Object aThis) {
        This = aThis;
    }

    public Object[] getArguments() {
        return arguments;
    }

    public void setArguments(Object[] arguments) {
        this.arguments = arguments;
    }

    public Object getReturnValue() {
        return returnValue;
    }

    public void setReturnValue(Object returnValue) {
        this.returnValue = returnValue;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
    }

    public Method getMethod() {
        // todo 缓存下method对象
        if (method == null) {
            int i = signature.indexOf('#');
            String md = signature.substring(i + 1);
            int i1 = md.indexOf('(');
            String m = md.substring(0, i1);
            String d = md.substring(i1);
            Method[] declaredMethods = clz.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {

                String name = declaredMethod.getName();
                if (!name.equals(m)) {
                    continue;
                }
                String methodDescriptor = Type.getMethodDescriptor(declaredMethod);
                if (methodDescriptor.equals(d)) {
                    method = declaredMethod;
                    return method;
                }
//                Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
//                int length = parameterTypes.length;


            }
        }

//        if (This == null) {
//            return null;
//        }
        /*
        if (method == null) {// todo 这里要改
            MethodVo methodVo = MethodVo.of(signature);
            String name = methodVo.getMethodName();
            String[] parameterStringTypes = methodVo.getParamTypes();

            Class<?> aClass = This.getClass();
            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                int length = parameterStringTypes.length;
                if (declaredMethod.getName().equals(name) && declaredMethod.getParameterTypes().length == length) {
                    for (int i = 0; i < length; i++) {
                        if (!declaredMethod.getParameterTypes()[i].getName().equals(parameterStringTypes[i])) {
                            break;
                        }
                        if (i == length - 1) {
                            method = declaredMethod;
                            break;
                        }
                    }
                }
            }
        }*/
        return method;
    }
}
